package com.wh.wisdomsite.xbox.server.controller.du;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wh.wisdomsite.xbox.common.util.ResManager;
import com.wh.wisdomsite.xbox.du.entity.DustAlarmConfig;
import com.wh.wisdomsite.xbox.du.service.IDustAlarmConfigService;
import com.wh.wisdomsite.xbox.pojo.du.DustAlarmConfigIn;
import com.wh.wisdomsite.xbox.server.manager.UserMapManager;
import com.wh.wisdomsite.xbox.server.util.UserUtils;
import com.wh.wisdomsite.xbox.utils.StringUtils;
import com.wh.wisdomsite.xbox.utils.json.JsonUtil_Org;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

/**
 * 扬尘警报预警参数配置controller
 * @author 刘旭
 * @version V1.0
 * @Package com.wh.wisdomsite.xbox.server.controller.du
 * @date 2018年08月06日 22:46
 * @Copyright 企业版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2018
 */
@Controller
@RequestMapping(value = "/dustAlarmConfig")
public class DustAlarmConfigController {
    private static final Log logger = LogFactory.getLog(DustAlarmConfigController.class);

    @Autowired
    private IDustAlarmConfigService iDustAlarmConfigService;

    /**
     * 跳转到扬尘警报预警参数配置
     *
     * @return
     */
    @RequiresPermissions("rules:du_dustAlarmConfig_page")
    @RequestMapping(value = "/index.do", method = RequestMethod.GET)
    public String index() {
        return "du/dustAlarmConfig/dustAlarmConfigList";
    }

    /**
     * 获取扬尘警报预警参数配置列表
     *
     * @param response
     * @param in
     */
    @RequiresPermissions("rules:du_dustAlarmConfig_find")
    @RequestMapping(value = "/dustAlarmConfigList.do")
    public void DustAlarmConfigList(HttpServletResponse response, DustAlarmConfigIn in) {
        try {
            logger.info("加载扬尘警报预警参数配置列表-->>开始");
            if (in.getPage() == 0) in.setPage(1);
            if (in.getRows() == 0) in.setRows(10);
            Page<DustAlarmConfig> page = new Page<DustAlarmConfig>();
            page.setCurrent(in.getPage());
            page.setSize(in.getRows());
            //设置条件
            EntityWrapper<DustAlarmConfig> ewDustAlarmConfig = new EntityWrapper<DustAlarmConfig>();
            ewDustAlarmConfig.setEntity(new DustAlarmConfig());
            if (StringUtils.isNotEmpty(in.getPrId())) {
                ewDustAlarmConfig.eq("d.prId", in.getPrId());
            }
            if (StringUtils.isNotEmpty(in.getCompanyId())) {
                ewDustAlarmConfig.eq("d.companyId", in.getCompanyId());
            }
            //若用户不是超级管理员根据用户的数据权限添加查询条件
            if(!UserUtils.isSysAdmin()){
                ewDustAlarmConfig = UserUtils.setEntityWrapper(ewDustAlarmConfig, "co", "off", "pr");
            }
            ewDustAlarmConfig.orderBy("createDate", false);
            Page<Map<String, Object>> alarmConfigPage = iDustAlarmConfigService.getMapsPage(page, ewDustAlarmConfig);
            JSONObject json = new JSONObject();
            JSONArray array = new JSONArray();
            if (alarmConfigPage.getRecords().size() > 0) {
                for(Map<String, Object> dustAlarmConfig : alarmConfigPage.getRecords()){
                    JSONObject object = new JSONObject(dustAlarmConfig);
                    if(dustAlarmConfig.get("company") != null){
                        object.put("company",new JSONObject(dustAlarmConfig.get("company")));
                    }
                    if(dustAlarmConfig.get("project") != null){
                        object.put("project",new JSONObject(dustAlarmConfig.get("project")));
                    }
                    array.put(object);
                }
            }
            json.put("total", alarmConfigPage.getTotal());
            json.put("rows", array);
            ResManager.getJsonOut(response).print(json);
            logger.info("加载扬尘警报预警参数配置列表-->>结束");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 扬尘警报预警参数配置详情
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("dustAlarmConfigDesc.do")
    public String DustAlarmConfigDesc(String id, HttpServletRequest request) {
        logger.info("扬尘警报预警参数配置详情-->>开始");
        Map<String , Object> dustAlarmConfig = iDustAlarmConfigService.getMapById(id);
        if(dustAlarmConfig.get("createBy") != null){
            dustAlarmConfig.put("createBy", UserMapManager.getUserLoginNameInMap(dustAlarmConfig.get("createBy").toString()));
        }
        if(dustAlarmConfig.get("updateBy") != null){
            dustAlarmConfig.put("updateBy", UserMapManager.getUserLoginNameInMap(dustAlarmConfig.get("updateBy").toString()));
        }
        request.setAttribute("dustAlarmConfig", dustAlarmConfig);
        logger.info("扬尘警报预警参数配置详情-->>结束");
        return "du/dustAlarmConfig/dustAlarmConfigDesc";
    }

    /**
     * 删除扬尘警报预警参数配置
     * @param id
     * @param response
     */
    @RequiresPermissions("rules:du_dustAlarmConfig_del")
    @RequestMapping(value = "delDustAlarmConfig.do", method = RequestMethod.GET)
    public void delDustAlarmConfig(String id, HttpServletResponse response) {
        try {
            logger.info("删除扬尘警报预警参数配置-->>开始");
            iDustAlarmConfigService.deleteById(id);
            logger.info("删除扬尘警报预警参数配置-->>结束");
            ResManager.getJsonOut(response).print(JsonUtil_Org.returnTrue("删除成功"));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("删除扬尘警报预警参数配置-->>Exception" + e.getMessage());
        }
    }

    /**
     * 跳转至添加页面
     *
     * @return
     */
    @RequestMapping("/toDustAlarmConfig.do")
    public ModelAndView toDustAlarmConfig(String id, HttpServletRequest request) {
        logger.info("添加扬尘警报预警参数配置跳转-->>开始");
        DustAlarmConfig dustAlarmConfig = new DustAlarmConfig();
        if (StringUtils.isNotEmpty(id)) {
            dustAlarmConfig = iDustAlarmConfigService.selectById(id);
        }
        request.setAttribute("dustAlarmConfig", dustAlarmConfig);
        logger.info("添加扬尘警报预警参数配置跳转-->>结束");
        return new ModelAndView("du/dustAlarmConfig/dustAlarmConfigOption");
    }

    /**
     * 更新或修改扬尘警报预警参数配置
     * @param dustAlarmConfig
     * @param response
     */
    @RequiresPermissions("rules:du_dustAlarmConfig_edit")
    @RequestMapping(value = "/save.do", method = RequestMethod.POST)
    public void save(DustAlarmConfig dustAlarmConfig,HttpServletResponse response) {
        try {
            if (StringUtils.isNotEmpty(dustAlarmConfig.getDacId())) {
                logger.info("修改扬尘警报预警参数配置-->>开始");
                //设置修改人
                dustAlarmConfig.setUpdateBy(UserUtils.getCurrentUser().getUsId());
                dustAlarmConfig.setUpdateDate(new Date());
                iDustAlarmConfigService.updateById(dustAlarmConfig);
                logger.info("修改扬尘警报预警参数配置-->>结束");
                response.getWriter().write(JsonUtil_Org.returnTrue("更新扬尘警报预警参数配置成功"));
            } else {
                logger.info("添加扬尘警报预警参数配置-->>开始");
                //设置创建人
                dustAlarmConfig.setCreateBy(UserUtils.getCurrentUser().getUsId());
                dustAlarmConfig.setCreateDate(new Date());
                iDustAlarmConfigService.insert(dustAlarmConfig);
                logger.info("添加扬尘警报预警参数配置-->>结束");
                response.getWriter().write(JsonUtil_Org.returnTrue("添加扬尘警报预警参数配置成功"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("添加扬尘警报预警参数配置-->>Exception" + e.getMessage());
        }
    }
}
